package com.hyz.service.impl;

import client.AssetClient;
import client.UserClient;
import com.hyz.pojo.request.ApprovalResultRequest;
import com.hyz.pojo.response.AssetSecondmentResponse;
import com.hyz.service.ApproveService;
import com.hyz.util.Conversion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.ResponseResult;
import pojo.asset.assetSecondment.AssetSecondment;
import pojo.asset.assetSecondment.AssetSecondmentResult;

import java.util.List;

/**
 * @author HGD
 * @date 2022/10/9 15:25
 */
@Service
public class ApproveServiceImpl implements ApproveService {
    @Autowired
    private AssetClient assetClient;
    @Autowired
    private UserClient userClient;
    @Autowired
    private Conversion conversion;

    @Override
    public ResponseResult<List<AssetSecondmentResponse>> apGetAssetSecondments() {
        int code = 200;
        // TODO: 获取用户id
        Long approveId = 1L;

        ResponseResult<List<Long>> result = userClient.getUsersByDepartment(approveId);
        if (result.getStatus() != code) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        List<Long> userId = result.getData();
        ResponseResult<List<AssetSecondment>> responseResult = assetClient.apGetAssetSecondments(userId);
        if (responseResult.getStatus() != code) {
            return new ResponseResult<>(responseResult.getStatus(), responseResult.getMsg());
        }
        List<AssetSecondment> assetSecondments = responseResult.getData();
        List<AssetSecondmentResponse> assetSecondmentResponse = conversion.getAssetSecondmentResponse(assetSecondments);
        return new ResponseResult<>(200, "获取该部门的所有借用申请", assetSecondmentResponse);
    }

    @Override
    public ResponseResult<List<AssetSecondmentResponse>> apUpdateAssetSecondment(ApprovalResultRequest request) {
        int code = 200;
        Long assetSecondmentId = request.getApprovalId();
        AssetSecondmentResult result = request.getResult() ? AssetSecondmentResult.APPROVAL_AGREE : AssetSecondmentResult.FAILED;

        ResponseResult<String> responseResult = assetClient.apUpdateAssetSecondment(assetSecondmentId, result);
        if (responseResult.getStatus() != code) {
            return new ResponseResult<>(responseResult.getStatus(), responseResult.getMsg());
        }
        return apGetAssetSecondments();
    }

}
